package com.session5;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import net.miginfocom.swing.MigLayout;

public class JTreeDemo extends JFrame implements TreeSelectionListener, TreeExpansionListener{
	private JTree tree;
	private DefaultMutableTreeNode root;
	private DefaultMutableTreeNode brMusic,brImage;
	private DefaultMutableTreeNode brRock,brBecthoven,brImage1,brImage2;
	
	public JTreeDemo(){
		// TODO Auto-generated constructor stub
	}
	public void createForm(){
//		this.brBecthoven = new DefaultMutableTreeNode("Becthoven");
//		this.brRock = new DefaultMutableTreeNode("Rock");
//		this.brImage1 = new DefaultMutableTreeNode("Image 1");
//		this.brImage2 = new DefaultMutableTreeNode("Image 2");
//		
//		this.brMusic = new DefaultMutableTreeNode("Music");
//		this.brImage = new DefaultMutableTreeNode("Image");
//		
//		this.brMusic.add(brBecthoven);
//		this.brMusic.add(brRock);
//		
//		this.brImage.add(brImage1);
//		this.brImage.add(brImage2);
//		
//		this.root = new DefaultMutableTreeNode("Root");
//		this.root.add(brMusic);
//		this.root.add(brImage);
//		this.tree = new JTree(this.root);
//		this.getContentPane().add(tree);
		
//		Them Doi Tuong Object Vao Jtree
		
		this.brBecthoven = new DefaultMutableTreeNode(new Student("Pham Van Viet", "Thanh Ha", 21));
		this.brRock = new DefaultMutableTreeNode(new Student("Pham Van An", "Thanh Hoa", 24));
		this.brImage1 = new DefaultMutableTreeNode(new Student("Pham Thi Thu", "Ha Giang", 24));
		this.brImage2 = new DefaultMutableTreeNode(new Student("Pham Van Hai", "Ha Giang", 31));
		
		this.brMusic = new DefaultMutableTreeNode("Music");
		this.brImage = new DefaultMutableTreeNode("Image");
		
		this.brMusic.add(brBecthoven);
		this.brMusic.add(brRock);
		this.brImage.add(brImage1);
		this.brImage.add(brImage2);
		
		this.root = new DefaultMutableTreeNode("root");
		this.root.add(brMusic);
		this.root.add(brImage);
		this.tree = new JTree(this.root);
		this.getContentPane().add(tree);
		//Dang Ki Su Kien
		this.tree.addTreeSelectionListener(this);
		this.tree.addTreeExpansionListener(this);
	}
	public void showForm(){
		this.setTitle("JCombo Demo");
		this.setSize(400,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	public static void main(String[] args) {
		JTreeDemo d  =new JTreeDemo();
		d.createForm();
		d.showForm();
	}
	@Override
	public void valueChanged(TreeSelectionEvent e) {
		TreePath p = e.getNewLeadSelectionPath();
		if(p!=null){
			System.out.println(p.getLastPathComponent());
		}
	}
	@Override
	public void treeCollapsed(TreeExpansionEvent e) {
		TreePath p = e.getPath();
		System.out.println(p.getLastPathComponent().toString());
		System.out.println(p.getPathCount());
	}
	@Override
	public void treeExpanded(TreeExpansionEvent e) {
		TreePath p = e.getPath();
		System.out.println(p.getLastPathComponent().toString());
		System.out.println(p.getPathCount());
	}

}
